package com.tengju.bff.interfaces.manage.member.resource;

import com.tengju.bff.interfaces.manage.member.resource.facade.ResourceFacade;
import com.tengju.bff.interfaces.manage.member.resource.vo.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

@RestController
@Api(tags = "会员管理-投放管理接口")
@RequestMapping(value = "manage/member/resource")
@Slf4j
@ManageEntrance
public class ResourceController {

    @Autowired
    private ResourceFacade resourceFacade;

    @RequiresRoles("sso")
    @ApiOperation(value = "添加资源", notes = "添加资源")
    @RequestMapping(value = "addResource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addResource(@RequestBody AddResourceForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(resourceFacade.addResource(form, staff));
        } catch (DomainException | ApplicationException e) {
            log.error("添加资源失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("添加资源异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "编辑资源", notes = "编辑资源")
    @RequestMapping(value = "editResource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> editResource(@RequestBody EditResourceForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(resourceFacade.editResource(form, staff));
        } catch (DomainException | ApplicationException e) {
            log.error("编辑资源失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("编辑资源异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "删除资源", notes = "删除资源")
    @RequestMapping(value = "delResource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> delResource(@RequestBody DelResourceForm form) {
        try {
            return ApiResponse.newSuccess(resourceFacade.delResource(form));
        } catch (DomainException | ApplicationException e) {
            log.error("删除资源失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("删除资源异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "分页查询资源", notes = "分页查询资源")
    @RequestMapping(value = "getByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<PageResourceVO> getResourceByPage(GetResourceForm form) {
        try {
            return ApiResponse.newSuccess(resourceFacade.getResourceByPage(form));
        } catch (DomainException | ApplicationException e) {
            log.error("分页查询资源失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("分页查询资源异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

}
